package cn.wolfcode.luowowo;

import cn.wolfcode.luowowo.website.interceptor.CheckLoginInterceptor;
import cn.wolfcode.luowowo.website.param.UserInfoArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@SpringBootApplication
public class Website implements WebMvcConfigurer {

    @Autowired
    private UserInfoArgumentResolver userInfoArgumentResolver;

    public static void main(String[] args) {
        //解决netty冲突
        System.setProperty("es.set.netty.runtime.available.processors", "false");
        SpringApplication.run(Website.class,args);
    }

    @Bean
    public CheckLoginInterceptor checkLoginInterceptor(){
        return  new CheckLoginInterceptor();
    }

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(checkLoginInterceptor())
                .addPathPatterns("/**")   //拦截所有
                .excludePathPatterns("/login.html")
                .excludePathPatterns("/regis.html")
                .excludePathPatterns("/userLogin")
                .excludePathPatterns("/userRegist")

                //静态资源放行
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/js/**")
        ;
    }

    /**
     * 添加自定义参数解析器
     * @param resolvers
     */
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userInfoArgumentResolver);
    }
}
